package com.kgc.scd.consumer;

import com.kgc.scd.bean.ArrivalUser;
import com.kgc.scd.contant.ArrivalRemindConstant;
import com.kgc.scd.service.NotifyProArriveService;
import com.kgc.scd.service.sendMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created On : 2022/6/16.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 到货提醒，消费者
 */
@Slf4j
@Component
public class ArrivalRemindConsumer {


    @Autowired
    private sendMailService sendMailService;

    @Autowired
    private NotifyProArriveService notifyProArriveService;

    /**
     * @author : nainai
     * @date   : 2022/6/16
     * @param  : [java.lang.String, java.lang.String]
     * @return : java.lang.String
     * @description : 到货提醒，消费消息
     */
    @JmsListener(destination = ArrivalRemindConstant.ARRIVAL_REMIND_QUEUE_NAME)
    @SendTo(ArrivalRemindConstant.ARRIVAL_REMIND_RESP_QUEUE_NAME)
    public List<ArrivalUser> consumeMsgFromQueueUseP2P(String proId){
        log.info("到货提醒，消费消息，商品：{}到货", proId);

        // 查询需要发送的用户
        List<ArrivalUser> arrivalUsers = notifyProArriveService.queryArrivalUserByProId(proId);
        log.info("需要发送的{}", arrivalUsers);

        // 给用户发送通知
        for (ArrivalUser a : arrivalUsers) {
            sendMailService.sendEmai("百联商城，商品到货提醒",
                    String.format("商品到货提醒：\n    您订阅的商品%s，已到货！", a.getProName()),
                    a.getUserEmail());
        }

        return arrivalUsers;
    }
}
